package com.tmzn.controller;

import com.alibaba.fastjson2.JSONObject;
import com.tmzn.constant.IotConstant;
import com.tmzn.domain.TDevicePlan;
import com.tmzn.entity.AjaxResult;
import com.tmzn.entity.param.DeviceParam;
import com.tmzn.jwt.JwtKit;
import com.tmzn.jwt.bean.MyIJwtAble;
import com.tmzn.queue.MsgQueue;
import com.tmzn.redis.RedisCache;
import com.tmzn.service.*;
import com.tmzn.transdata.RpcResult;
import com.tmzn.transdata.entity.MainBoard;
import com.tmzn.transdata.entity.PlanCharge;
import com.tmzn.web.annotation.Anonymous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private ITDevicePlanService itDevicePlanService;

    @Autowired
    private DeviceControlerService deviceControlerService;

    @Autowired
    private ITDeviceRecordService itDeviceRecordService;

    @Autowired
    private ITChargeRecordService itChargeRecordService;

    @Autowired
    private DeviceService deviceService;


    @Autowired
    private RedisCache redisCache;

    @Autowired
    private MsgQueue msgQueue;
    @RequestMapping("/cancelPlan/{id}")
    public RpcResult cancelPlan(@PathVariable("id") Long planId){
        RpcResult rpcResult = itDevicePlanService.cancelPlan(planId);
        return rpcResult;
    }

    @RequestMapping("/addMsg")
    @Anonymous
    public String addMsg(@RequestParam("msg") String msg){
        msgQueue.add(msg);
        return "ok";
    }
    @RequestMapping("/testRedis")
    public String testRedis(@RequestParam("msg") String msg){
        redisCache.setCacheObject(IotConstant.PLAN_CHARGE_DIR_REDIS_KEY,"22");
        return redisCache.getCacheObject(IotConstant.PLAN_CHARGE_DIR_REDIS_KEY);
    }

    @GetMapping("/testrpc")
    @Anonymous
    public RpcResult testRpc(){
        return null;
    }

    @RequestMapping("/getPlanInfo")
    public AjaxResult getPlanInfo(String deviceId,String port){
        Integer userId = JwtKit.getLoginUser().getId();
        TDevicePlan tDevicePlan = deviceService.getPlanInfo(deviceId,port,userId);
        return  AjaxResult.success(tDevicePlan);
    }

    @RequestMapping("/getDeviceStatusInfo")
    public AjaxResult getDeviceStatusInfo(String deviceId,Integer cmdType){
        Integer userId = JwtKit.getLoginUser().getId();
        Map<String, Object> deviceStatusInfo = deviceService.getDeviceStatusInfo(deviceId, cmdType);
        return  AjaxResult.success(deviceStatusInfo);
    }


    @RequestMapping("/planCharge")
    public AjaxResult planCharge(@RequestBody DeviceParam deviceParam){
        String ccid = deviceParam.getCcid();
        String deviceId = deviceParam.getDeviceId();
        Integer port = deviceParam.getPort();
        PlanCharge planCharge = new PlanCharge();
        planCharge.setMoney(50000);
        planCharge.setPort(port);
        planCharge.setTime(Integer.parseInt(deviceParam.getTime()));
        deviceControlerService.planCharge(deviceId,ccid,planCharge);
        return  AjaxResult.success();
    }
    @RequestMapping("/cancelPlanCharge")
    public AjaxResult cancelPlanCharge(@RequestBody DeviceParam deviceParam){
        String ccid = deviceParam.getCcid();
        String deviceId = deviceParam.getDeviceId();
        Integer port = deviceParam.getPort();
        deviceControlerService.cancelChargePlan(deviceId,ccid,port);
        return  AjaxResult.success();
    }



    @PostMapping("/deviceList")
    public RpcResult deviceList(@RequestParam Integer status){
        List<Map<String, Object>> maps = deviceService.listDevice(status);
        return RpcResult.success(maps);
    }

    @PostMapping("/getDeviceInfo")
    public AjaxResult getDeviceInfo(@RequestParam("qrcode") String qrcode){
        Map<String, Object> deviceInfoByQrcode = deviceService.getDeviceInfoByQrcode(qrcode);
        return AjaxResult.success(deviceInfoByQrcode);
    }

    @PostMapping("/sendPortDetailCmd")
    public AjaxResult sendPortDetailCmd (@RequestBody DeviceParam deviceParam) {
        deviceControlerService.sendPortDetailCmd(deviceParam);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return AjaxResult.success(sdf.format(date));
    }

    @PostMapping("/getPortDetail")
    public AjaxResult getPortDetail (@RequestBody DeviceParam deviceParam) {
        JSONObject portDetail = itDeviceRecordService.getPortDetail(deviceParam);
        return AjaxResult.success(portDetail);
    }

    @PostMapping("/startCharge")
    public AjaxResult startCharge(@RequestBody DeviceParam deviceParam) {
        // 对电流值进行处理
        MyIJwtAble loginUser = JwtKit.getLoginUser();
        itChargeRecordService.startCharge(deviceParam.getDeviceId(),deviceParam.getCcid(),loginUser.getId(),deviceParam.getPort());
        return AjaxResult.success();
    }

    @PostMapping("/stopCharge")
    public AjaxResult stopCharge(@RequestBody DeviceParam deviceParam) {
        // 对电流值进行处理
        deviceControlerService.stopCharge(deviceParam.getDeviceId(),deviceParam.getCcid(),deviceParam.getPort());
        return AjaxResult.success();
    }

    /**
     * 1.在开始充电后，需要做一个定时任务检测是否结束充电
     *
     * 2.在普通情况下 检查枪是否变动  暂定 感觉是枪口连上了 才有
     * @return
     */
    @PostMapping("/statusChange")
    public AjaxResult checkStatusChange(@RequestBody DeviceParam deviceParam){
        JSONObject object = itDeviceRecordService.checkStatus(deviceParam);
        return AjaxResult.success(object);
    }

    @PostMapping("/setCurrent")
    public AjaxResult setCurrent(@RequestBody DeviceParam deviceParam,@RequestParam("current") Integer current) {
        MainBoard mainBoard = new MainBoard();
        mainBoard.setMaxCurrent(current);
        String deviceId = deviceParam.getDeviceId();
        String ccid = deviceParam.getCcid();
        deviceControlerService.setMainBoard(deviceId,ccid,mainBoard);
        return AjaxResult.success(current);
    }

    @PostMapping("/sendMainboardCmd")
    public AjaxResult sendMainboardCmd(@RequestBody DeviceParam deviceParam) {

        deviceControlerService.getMainBoard(deviceParam.getDeviceId(),deviceParam.getCcid());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return AjaxResult.success(sdf.format(date));
    }

    @PostMapping("/getMainboardConfig")
    public AjaxResult getMainboardConfig(@RequestBody DeviceParam deviceParam) {
        JSONObject mainboardConfig = itDeviceRecordService.getMainboardConfig(deviceParam);
        return AjaxResult.success(mainboardConfig);
    }

    @PostMapping("/resetMainboard")
    public AjaxResult resetMainboard(@RequestBody DeviceParam deviceParam) {
        // 恢复默认配置
        deviceControlerService.reset(deviceParam.getDeviceId(),deviceParam.getCcid());
        return AjaxResult.success();
    }

    @PostMapping("/restartMainboard")
    public AjaxResult restartMainboard(@RequestBody DeviceParam deviceParam) {
        // 重启充电桩
        deviceControlerService.restart(deviceParam.getDeviceId(),deviceParam.getCcid());
        return AjaxResult.success();
    }

}
